package com.myhome.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.myhome.domain.IdProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.*;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

/**
 * @Author QYiXin
 * @Date 2021 10 15 19 04
 **/
@Configuration
@EnableConfigurationProperties(IdProperties.class)
public class IdAutoConfig {
    public static IdProperties idProperties;
    public IdAutoConfig(IdProperties idProperties) {
        IdAutoConfig.idProperties = idProperties;
    }
    public static boolean checkUserId(String realName,String cardNum){
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.set("Authorization","APPCODE "+idProperties.getAppCode());
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> resultStr = restTemplate.exchange(
                String.format(idProperties.getUrl(), cardNum, realName),
                HttpMethod.GET,
                new HttpEntity<>(null, httpHeaders),
                String.class
        );
        if (resultStr.getStatusCode() == HttpStatus.OK) {
            String body = resultStr.getBody();
            JSONObject jsonObject = JSON.parseObject(body);
            String status = jsonObject.getString("status");
            if ("01".equals(status)) {
                return true;
            }
        }
        return false;
    }
}
